package com.weiweiqin.controller.home;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.weiweiqin.vo.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.weiweiqin.constants.Constants;
import com.weiweiqin.model.Color;
import com.weiweiqin.service.BrandsService;
import com.weiweiqin.service.ColorService;
import com.weiweiqin.service.GoodsService;
import com.weiweiqin.service.PicService;
import com.weiweiqin.utils.CompressPicUtil;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * @author weiweiqin
 */
@Controller
@RequestMapping("front/upload")
public class FUploadController {

    /**
     * 图片压缩质量
     */
    private static final float QUALITY = 0.97f;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private PicService picService;

    @Autowired
    private ColorService colorService;

    @Autowired
    private BrandsService brandsService;

    @RequestMapping("download.do")
    public void downloadFile(String fileName, HttpServletRequest request,
                             HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");

        response.setHeader("Content-Disposition", "attachment;fileName="
                + fileName);
        try {
            String webapps = getTomcatWebappsPath(request);
            String filePath = webapps + File.separator
                    + Constants.FileInfo.FILE_PATH;
            File file = new File(fileName);
            InputStream inputStream = new FileInputStream(filePath + "/" + file);
            OutputStream os = response.getOutputStream();
            byte[] b = new byte[1024];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
            }
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("downloadCompress.do")
    public void downloadCompress(String fileName, HttpServletRequest request,
                                 HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");

        response.setHeader("Content-Disposition", "attachment;fileName="
                + fileName);
        try {
            String webapps = getTomcatWebappsPath(request);
            String filePath = webapps + File.separator
                    + Constants.FileInfo.COMPRESS_FILE_PATH;
            File file = new File(fileName);
            InputStream inputStream = new FileInputStream(filePath + "/" + file);
            OutputStream os = response.getOutputStream();
            byte[] b = new byte[1024];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
            }
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取tomcat webapps目录
     *
     * @param request
     * @return
     */
    private static String getTomcatWebappsPath(HttpServletRequest request) {
        String result = "";
        String tomcatRoot = request.getSession().getServletContext()
                .getRealPath("/");
        int indexOf = tomcatRoot.indexOf("webapps");
        if (indexOf != -1) {
            result = tomcatRoot.substring(0, indexOf + 7);
        } else {
            result = "/Users/wwq/projects/";
        }
        return result;
    }

    //改变历史颜色图片的压缩图片大小
    @RequestMapping("setCompressColor.do")
    public void setCompressColor(HttpServletRequest request,
                                 HttpServletResponse response) {

        String webapps = getTomcatWebappsPath(request);
        // 原始图片存放路径
        String filePath = webapps + File.separator
                + Constants.FileInfo.FILE_PATH;

        // 压缩图片存放路径
        String compressFilePath = webapps + File.separator
                + "colorPic";
        int number = 0;

        //删除原始图片
        File folder = new File(filePath);
        File[] files = folder.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            String picName = file.getName();
            List<Color> colorList = colorService.getByPicName(picName);
            if (null != colorList && !colorList.isEmpty()) {
                // 压缩图片
                CompressPicUtil
                        .createThumbnail(filePath + "/" + picName,
                                compressFilePath + "/" + picName, QUALITY, 0,
                                50);
                number++;

            }
        }
        System.out.println("number=" + number);
    }

    @RequestMapping(value = "/upload.do", method = RequestMethod.POST)
    @ResponseBody
    public String upload(HttpServletRequest request,
                         HttpServletResponse response) {
        Result result = new Result();
        Map<String, Object> mp = new HashMap<String, Object>();

        MultipartHttpServletRequest multipartHttpservletRequest = (MultipartHttpServletRequest) request;
        // Map<String, MultipartFile> fileMap =
        // multipartHttpservletRequest.getFileMap();
        MultipartFile multipartFile = multipartHttpservletRequest
                .getFile("file");
        String originalFileName = multipartFile.getOriginalFilename();

        int index = originalFileName.indexOf(".");

        // 通过文件名给子文件排序
        int orderKey = 0;
        try {

            String fileName = originalFileName.substring(0, index);
            orderKey = Integer.valueOf(fileName);
        } catch (Exception e) {
            orderKey = 0;
        }
        originalFileName = originalFileName.substring(index);
        String webapps = getTomcatWebappsPath(request);
        // 原始图片存放路径
        String filePath = webapps + File.separator
                + Constants.FileInfo.FILE_PATH;
        // 压缩图片存放路径
        String compressFilePath = webapps + File.separator
                + Constants.FileInfo.COMPRESS_FILE_PATH;
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        File compressFile = new File(compressFilePath);

        if (!compressFile.exists()) {
            compressFile.mkdirs();
        }
        String newFileName = System.currentTimeMillis() + "_"
                + originalFileName;
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file + "/"
                    + newFileName);
            fileOutputStream.write(multipartFile.getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();
            // 压缩图片
            Map<String, Object> compressResult = CompressPicUtil
                    .createThumbnail(filePath + "/" + newFileName,
                            compressFilePath + "/" + newFileName, QUALITY, 249,
                            0);
            // 压缩图片成功
            if (Boolean.valueOf(compressResult.get("success").toString())) {
                mp.put("height", compressResult.get("height").toString());
                mp.put("width", compressResult.get("width").toString());
                mp.put("compressWidth", compressResult.get("compressWidth")
                        .toString());
                mp.put("compressHeight", compressResult.get("compressHeight")
                        .toString());
            } else {
                result.setResult(false);
                result.setErrorMsg(mp.get("errorMsg").toString());
                return result.toJson();
            }
        } catch (FileNotFoundException e) {
            result.setResult(false);
            result.setErrorMsg("没有找到图片文件");
            return result.toJson();
        } catch (IOException e) {
            result.setResult(false);
            result.setErrorMsg("上传图片失败...");
            return result.toJson();
        }
        result.setResult(true);
        mp.put("name", newFileName);
        mp.put("oriName", originalFileName);
        mp.put("orderKey", orderKey);
        mp.put("size", multipartFile.getSize());
        result.setObj(mp);
        return result.toJson();
    }
}